<?php
declare(strict_types=1);

namespace app\service;

use app\model\AdminLoginLog;
use think\facade\Request;

class AdminLogService
{
    /**
     * 记录登录日志
     */
    public function recordLogin($admin_id, $status = 1, $remark = '')
    {
        AdminLoginLog::create([
            'admin_id' => $admin_id,
            'ip' => Request::ip(),
            'user_agent' => Request::header('user-agent'),
            'status' => $status,
            'remark' => $remark,
            'create_time' => date('Y-m-d H:i:s')
        ]);
    }
    
    /**
     * 获取登录日志
     */
    public function getLoginLogs($admin_id, $limit = 10)
    {
        return AdminLoginLog::where('admin_id', $admin_id)
            ->order('id', 'desc')
            ->limit($limit)
            ->select();
    }
    
    /**
     * 清理过期日志
     */
    public function cleanLogs($days = 30)
    {
        $time = date('Y-m-d H:i:s', strtotime("-{$days} days"));
        AdminLoginLog::where('create_time', '<', $time)->delete();
    }
} 